﻿package screen.component
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.utils.setInterval;
	
	import helper.Services;
	import helper.ValueObject;
	
	//import mx.utils.NameUtil;

	public class Main extends Popup
	{
		private const _BLOCK_NAME:String="block";
		private var _service:Services;
		private var _current_time:Number =100;
		private var _step:Number = 3;
		public static var instance:Main;
		public function Main()
		{
			init();
		}
		private function init():void{
			_service = Services.getInstance();
			_popup = ValueObject.T1_RESOURCE.getExportedAsset("mainPopup") as MovieClip;
			for(var i:Number =0;i<4;i++){
				var main_block:MainBlock = new MainBlock(i);
				main_block.x = -_popup.width/2 + (i%2)*241+10;
				main_block.y =-_popup.height/2 +  Math.floor(i/2)*242-10;
				main_block.name = _BLOCK_NAME + i;
				_popup.addChild(main_block);
			}
			MovieClip(_popup.timeMc).gotoAndStop(1);
			popupInit(_popup);
			reset();
			setInterval(setTimeProgress,1000);
		}
		private function setTimeProgress():void{
			for(var i:Number =0;i<4;i++){
				var main_block:MainBlock = _popup.getChildByName(_BLOCK_NAME + i) as MainBlock;
				if(!main_block.buyGift){
					break;
				}
				if(i==3){
					_current_time = -1;
				}
			}
			_current_time -=_step;
			if(_current_time < 0){
				reset();
				_current_time = 100;
				//trace("reset");
			}
			MovieClip(_popup.timeMc).gotoAndStop(Math.floor(_current_time));
		}
		private function reset():void{
			_service.getGifts(getGiftComplete,getGiftError);
			_service.getCards(getCardComplete,getCardError);
		}
		private function getGiftComplete(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getGiftComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getGiftComplete);
			var arr:Array = JSON.decode(e.target.data);
			var leng:Number = arr.length;
			for(var i:Number =0;i<leng;i++){
				var main_block:MainBlock = _popup.getChildByName(_BLOCK_NAME + i) as MainBlock;
				main_block.updateGifts(arr[i]);
			}
		}
		private function getGiftError(e:IOErrorEvent):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getGiftComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getGiftComplete);
		}
		private function getCardComplete(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getCardComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getCardError);
			var arr:Array = JSON.decode(e.target.data);
			var leng:Number = arr.length;
			for(var i:Number =0;i<leng;i++){
				var main_block:MainBlock = _popup.getChildByName(_BLOCK_NAME + i) as MainBlock;
				main_block.updateATMCard(arr[i]);
			}
		}
		private function getCardError(e:IOErrorEvent):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getCardComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getCardError);
		}
		public function set step(value:Number):void{
			_step = value;
		}
		
	}
}